package logic

import (
	"context"
	"home/model/mysql"

	"home/homerpc/homerpc"
	"home/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HomeListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHomeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HomeListLogic {
	return &HomeListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表
func (l *HomeListLogic) HomeList(in *homerpc.HomeListRequest) (*homerpc.HomeListResponse, error) {
	// todo: add your logic here and delete this line

	var homes []mysql.Course
	mysql.DB.Debug().Scopes(mysql.Paginate(int(in.Page), int(in.PageSize))).Find(&homes)

	var HomeList []*homerpc.HomeList

	for _, home := range homes {
		list := homerpc.HomeList{
			Title: home.Title,
			Info:  home.Info,
			Image: home.Image,
			Uid:   int64(home.Uid),
			Price: float32(home.Price),
		}

		HomeList = append(HomeList, &list)
	}

	return &homerpc.HomeListResponse{
		List: HomeList,
	}, nil
}
